﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using System.IO;
using DevExpress.XtraEditors;
using Model;
using BartonExtension;

namespace Backend
{
    public partial class FrmActivity : DevExpress.XtraEditors.XtraForm
    {
        private List<NewStore> stores;

        public Activity activity { get; set; }
        public DialogResult result { get; set; }
        public FrmActivity()
        {
            InitializeComponent();
            Init();
        }

        public void Init()
        {
            result = System.Windows.Forms.DialogResult.Cancel;
            WebServiceHelper.LoadStores((s, e) =>
                {
                    if(e!= null)
                    {
                        XtraMessageBox.Show(e.Message);
                        return;
                    }
                    else
                    {
                        stores = s;
                        lkStore.Invoke(new Action(() =>
                            {
                                lkStore.Properties.DataSource = stores;
                            }));
                    }
                });
        }

        private void btnAddImage_Click(object sender, EventArgs e)
        {
            using(OpenFileDialog dialog = new OpenFileDialog())
            {
                if(dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    string filename = dialog.FileName;
                    if(!lstImages.Items.Contains(filename))
                    {
                        lstImages.Items.Add(filename);
                    }
                }
            }
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            if (lstImages.SelectedIndex == -1) return;
            lstImages.Items.RemoveAt(lstImages.SelectedIndex);
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            activity = new Activity() { ID = -1 };
            int store_id = lkStore.EditValue.toInt32().Value;
            int index = lkStore.ItemIndex;
            NewStore store = stores[index];
            StoreCatalog catalog = store.catalogs[cmbCatalog.SelectedIndex];
            activity.store_id = store.ID;
            activity.sale_desc = memoDesc.Text;
            activity.discount_rate = spinDiscount.EditValue.toFloat().Value;
            activity.time_from = dateFrom.EditValue.ToString();
            activity.time_to = dateTo.EditValue.ToString();

            string images = "";
            foreach(string filename in lstImages.Items)
            {
                string title = store.title;
                string path = @"{0}\{1}\{2}\{3:yyyymmdd}\".FormatWith(AddressPool.Workspace, "data",store.title, DateTime.Now);
                string fname = "{0:yyyymmddhhmmss}.{1}".FormatWith(DateTime.Now, Path.GetExtension(filename));
                if(!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                File.Copy(filename, path + fname);
                images += path + fname + ",";
            }
            images = images.TrimEnd(',');
            activity.images = images;
            result = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        private void lkStore_Properties_EditValueChanged(object sender, EventArgs e)
        {
            NewStore store = stores[lkStore.ItemIndex];
            cmbCatalog.Properties.Items.Clear();
            cmbCatalog.SelectedIndex = -1;
            cmbCatalog.Properties.Items.AddRange(store.catalogs.Select(c => c.catalog).Select(c => c.title_en).ToArray());
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            result = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }
    }
}